---
output: 
    pdf_document:
        keep_tex: yes
title: "Network"
geometry: margin=1in
mainfont: cochineal
fontsize: 11pt
linestretch: 1.15
endnote: no
sansitup: no
graphics: yes
toc: yes

header-includes:
- \usepackage{float} #use the 'float' package
- \floatplacement{figure}{H} #make every figure with caption = h
- \usepackage{graphicx}
- \usepackage{longtable}
- \LTcapwidth=.95\textwidth
- \linespread{1.05}
- \usepackage{hyperref}
- \usepackage{booktabs}
- \usepackage{subfig}
- \renewcommand{\figurename}{Figure B.}
- \makeatletter
- \def\fnum@figure{\figurename\thefigure}
- \makeatother


---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r, }
# Load packages
library(tidyverse)
library(broom)
library(kableExtra)
library(igraph)

# Load data
network_base <- read_csv2('./replication_files/data/data/final_data/network_base.csv')

```


```{r, }
# The function takes the network_base data calculates the 5 percentile distance
# and compares the distance of each country to the 5th percentile. If it is  above it drops the country

id_function <- function(country, year) {
  
  network_base %>% 
    filter(year %in% seq(t_year-10:t_year)) %>% 
    group_by(ctry1, ctry2) %>%
    summarize(mean_dist = mean(IdealPointDistance)) %>%
    drop_na() %>%
    group_by(ctry1) %>%
    # Here we vary the cut-off point
    # Top 5th percentile
    mutate(quantile_10 = quantile(mean_dist, 0.05)) %>%
    filter(mean_dist < quantile_10) 
  
}
```


```{r, }
c <- network_base %>% filter(treated_country == 1) %>% 
  select(ctry1, year) %>%
  filter(year == 2013) %>%
  distinct()

countries <- c$ctry1
t_year <- unique(c$year)

```

```{r, , fig.show = 'hide'}

# Container
datalist = list()

# For loop for data with ID
for (i in countries) {
    # ... make some data
    dat <- id_function(i)
    #dat$i <- i  # maybe you want to keep track of which iteration produced it?
    datalist[[i]] <- dat # add it to your list
}

data <- bind_rows(datalist) %>% distinct()

data_saved <- data

# Here we vary the 0.25 threshold
data <- data %>% filter(mean_dist < 0.25)

ggplot(data, aes(mean_dist)) +
  geom_histogram(bins = 50, color = 'black', fill = 'gray80') +
  theme_minimal() +
  labs(x = 'Average Distance',
       y = 'Count')

#ggsave('./replication_files/analysis/analysis/graphs/network/distribution-of-distances.pdf')

```


## Distances between countries

We create a network based on the average distance between UNGA Idealponts over a ten year period, prior to the treatment, i.e. the accession to the BRI. The idea is, that we need to identify voting blocs to accurately identify the sensitive changes in ideal points. To do so, we calculate the average distance and select those countries that are within the 5 most proximate percentile to the country under consideration. 

To give an example, we calculate the average distance of each country to Hungary and select the countries that are within the top 5th percentile.

```{r, }
test <- knitr::kable(data %>% filter(ctry1 == 'Hungary'),
             caption = "Table 1: Most proximate countries to Hungary",
             col.names = c("Country 1", " Country 2", "Average Distance", "Fifth percentile"),
             booktabs = TRUE, 
             format = "latex") %>%
  kable_styling(font_size = 8)

```

It should be noted, however, that we limit the average distance to < 0.25. Larger distances may be included in the data, simply because there are countries that vote very differently to all other states. Distances within 1/4 of a point seem reasonable, ultimately though, this threshold is arbitrary. 

## Networks

Based on the above-calculated distances we create a network of voting blocs in the UNGA, based on their ideal point distances between 2004 and 2013. 

```{r}
# Relocate variables first to have connected entities
data <- data %>% select(-quantile_10) %>% relocate(ctry1, ctry2, everything())

# Create the network object
network_full <- graph_from_data_frame(d = data, directed = F)

# Calculate the clusters
#cluster_full <- cluster_optimal(network_full, weights = NULL)
cluster_full <- network_full  %>% cluster_walktrap() 

# Storing the cluster information in a dataframe
# We will use this later to specify the clusters across the years
cluster_data <- as_tibble(cbind(cluster_full$names, cluster_full$membership)) %>% 
  arrange(V2) %>%
  rename('ctry1' = 'V1', 
         'cluster' = 'V2') %>%
  mutate(cluster = as.numeric(cluster))

degree <- degree(network_full)

number_of_ties <- enframe(degree) %>%
   unnest
    
# Here we vary the the minimum number of ties
cluster_data <- left_join(cluster_data, number_of_ties,
                          by = c('ctry1' = 'name')) %>% 
  filter(value > 3)

#ggsave('./replication_files/analysis/analysis/graphs/network/distribution-of-distances.pdf')

#write_csv2(cluster_data, './replication_files/data/data/final_data/cluster.csv')

cluster_data <- read_csv2('./replication_files/data/data/final_data/cluster.csv')

```


```{r, message = F, comment = F}

# Create the voting network figure
V(network_full)$frame.color <- "white"
E(network_full)$arrow.mode <- 0


#### Reproduces Figure 4 in the Manuscript (note that the plot may look slightly different as the same network can be arranged in many different ways, however, the voting communities remain the same irrespective of the configuration of the network) ####

# Create the network figure
#pdf('./replication_files/analysis/analysis/graphs/network/full-network.pdf')
plot.igraph(network_full,  vertex.color=membership(cluster_full), vertex.size = 7, vertex.label.cex=0.85)
#dev.off()

```

We first plot a full network, including all countries. It displays, however a large cluster in the middle. To further specify the voting blocs we subset this large cluster, re-create a network, a sub-network, and use again a community detection algorithm to identify clusters.

```{r, fig.cap ='Network of voting clusters (10 year period 2004-2013)',  out.width='100%', }
#knitr::include_graphics('./replication_files/analysis/analysis/graphs/network/full-network.pdf')
```

```{r, }
#end
```
